package com.zhkj.txg.module.mine.ui

import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.LogUtils
import com.google.android.material.tabs.TabLayoutMediator
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.R
import com.zhkj.txg.base.CustomApplication
import com.zhkj.txg.module.mine.adapter.CollectionAdapter
import com.zhkj.txg.module.mine.vm.CollectionViewModel
import kotlinx.android.synthetic.main.activity_collection.*
import kotlinx.android.synthetic.main.activity_collection.refreshCollection
import kotlinx.android.synthetic.main.activity_collection.rvCollection

class CollectionActivity : ViewModelActivity<CollectionViewModel>() {
    override fun setViewModel(): CollectionViewModel = createViewModel()
    override fun statusTitleBar(): View? = titleCollection
    override val layoutResId: Int = R.layout.activity_collection

    private val goodsFragment: CollectionGoodsFragment by lazy {
        CollectionGoodsFragment.newInstance(CollectionGoodsFragment.TYPE_GOODS)
    }
    private val shopFragment: CollectionGoodsFragment by lazy {
        CollectionGoodsFragment.newInstance(CollectionGoodsFragment.TYPE_SHOPS)
    }

    private val collectionAdapter: CollectionAdapter = CollectionAdapter()

    override fun initUI() {
        with(rvCollection) {
            (itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
            layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this@CollectionActivity)
            adapter = collectionAdapter
        }
        refreshCollection.setOnRefreshListener {
            initData()
        }
    }

    override fun initData() {
        viewModel.collectionList(AccountManager.getToken(), CustomApplication.getShopId().toString(),true)
    }

    override fun initViewModel() {

        // 当前页面列表数据刷新观察者
        viewModel.collectionLiveData.observe(this, Observer { response ->
            when (response.status) {
                HttpResponseStatus.Status.START -> Unit
                HttpResponseStatus.Status.SUCCESS -> response.response?.let {
//                    if (viewModel.type == CollectionGoodsFragment.TYPE_GOODS) {
//                    }
                    // 判断加载状态
                    if (!it.loadMore) refreshCollection.finishRefresh()
                    else if (it.hasMore) collectionAdapter.loadMoreModule?.loadMoreComplete()
                    else collectionAdapter.loadMoreModule?.loadMoreEnd()
                    // 数据填充
                    if (it.loadMore) collectionAdapter.addData(it.data)
                    else collectionAdapter.setNewData(it.data)
                }
                HttpResponseStatus.Status.FAILURE -> {
                    response.exception?.printStackTrace()
                    toast(response.statusTip)
                    when {
                        viewModel.page <= 1 -> refreshCollection.finishRefresh(false)
                        viewModel.type == CollectionGoodsFragment.TYPE_GOODS -> collectionAdapter.loadMoreModule?.loadMoreFail()
                        else -> collectionAdapter.loadMoreModule?.loadMoreFail()
                    }
                }
            }
        })
    }

    /**
     * 根据fragment刷新编辑按钮的状态
     */
    fun refreshTitleAndBottom(fragment: CollectionGoodsFragment) {

    }

    private fun getCurrentFragment(position: Int): CollectionGoodsFragment = when (position) {
        0 -> goodsFragment
        else -> shopFragment
    }
}

data class CollectStatusChangeEvent(
    val type: String,
    val checkStatus: CheckStatusEntity
)